package cn.zch.soft.util.hutool.logistical;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class kuaidi100 {
  public static void main(String[] agrs) {
    //2075704675
    //43271181412711779
    //9781802244504
    String id = StaticInfo.apiIdZch;
    String com = "ems";
    String nu = "198277571961";
    String show = "0";
    String muti = "1";
    String order = "desc";
    //String result = getMyLogisticalInfoByKuaiDi100(id, com, nu, show, muti, order);
    //Console.log(result);
    //Console.log("/*************2************/");
    // getMyLogisticalInfoBySelefMethod(id, com, nu, show, muti, order);
    //Console.log("/*************3************/");
    getMyLogisticalInfoByDebugUrl(nu);
  }
  public static String getMyLogisticalInfoByKuaiDi100(String id, String com, String nu, String show, String muti, String order) {
    try {
      //String urlStr = "http://api.kuaidi100.com/api?id=XXXX&com=tiantian&nu=11111&show=2&muti=1&order=desc";
      //  id	String	是	身份授权key，请 快递查询接口 进行申请（大小写敏感）
      //  com	String	是	要查询的快递公司代码，不支持中文，对应的公司代码见《API URL 所支持的快递公司及参数说明》和《支持的国际类快递及参数说明》。如果找不到您所需的公司，请发邮件至 kuaidi@kingdee.com 咨询（大小写不敏感）
      //  nu	String	是	要查询的快递单号，请勿带特殊符号，不支持中文（大小写不敏感）
      //  valicode	String	是	已弃用字段，无意义，请忽略。
      //  show	String	是	返回类型：0：返回json字符串，1：返回xml对象，2：返回html对象，3：返回text文本。如果不填，默认返回json字符串。
      //  muti	String	是	返回信息数量：1:返回多行完整的信息，0:只返回一行信息。不填默认返回多行。
      //  order	String	是	排序：desc：按时间由新到旧排列，asc：按时间由旧到新排列。不填默认返回倒序（大小写不敏感）
      //http://api.kuaidi100.com/api?id=[]&com=[]&nu=[]&valicode=[]&show=[0|1|2|3]&muti=[0|1]&order=[desc|asc]
      String urlStr = String.format("http://api.kuaidi100.com/api?id=%s&com=%s&nu=%s&show=%s&muti=%s&order=%s", id, com, nu, show, muti, order);
      Console.log(urlStr);
      URL url = new URL(urlStr);
      URLConnection con = url.openConnection();
      con.setAllowUserInteraction(true);
      InputStream urlStream = url.openStream();
      Console.log(urlStream);
      HttpRequest.get(urlStr);
      String type = con.guessContentTypeFromStream(urlStream);
      String charSet = null;
      if (type == null) {
        type = con.getContentType();
      }
      if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) {
        return null;
      }
      if (type.indexOf("charset=") > 0) {
        charSet = type.substring(type.indexOf("charset=") + 8);
      }
      byte b[] = new byte[10000];
      int numRead = urlStream.read(b);
      String content = new String(b, 0, numRead);
      while (numRead != -1) {
        numRead = urlStream.read(b);
        if (numRead != -1) {
          //String newContent = new String(b, 0, numRead);
          String newContent = new String(b, 0, numRead, charSet);
          content += newContent;
        }
      }
      System.out.println("content:" + content);
      urlStream.close();
      return content;
    }
    catch (MalformedURLException e) {
      e.printStackTrace();
      return e.getMessage();
    }
    catch (IOException e) {
      e.printStackTrace();
      return e.getMessage();
    }
  }
  public static void getMyLogisticalInfoBySelefMethod(String id, String com, String nu, String show, String muti, String order) {
    //http://api.kuaidi100.com/api?id=[]&com=[]&nu=[]&valicode=[]&show=[0|1|2|3]&muti=[0|1]&order=[desc|asc]
    String urlStr = String.format("http://api.kuaidi100.com/api?id=%s&com=%s&nu=%s&show=%s&muti=%s&order=%s", id, com, nu, show, muti, order);
    Console.log(urlStr);
    HttpResponse response = HttpRequest.get(urlStr).execute();
    String body = response.body();
    Console.log(body);
    JSONObject json2 = JSONObject.fromObject(body);
    Console.log("响应data内容=2" + json2);
    Iterator iterator = json2.keys();
    while (iterator.hasNext()) {
      String key = (String) iterator.next();
      String value = json2.getString(key);
      Console.log(key + " " + value);
    }
  }
  public static void getMyLogisticalInfoByDebugUrl(String id) {
    //https://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=9781802244504
    //https://www.kuaidi100.com/query?type=youzhengguonei&postid=9781802244504&temp=0.6999168501241886
    String urlStr1 = String.format("https://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=%s", id);
    HttpResponse response = HttpRequest.get(urlStr1).execute();
    String body = response.body();
    JSONObject json2 = JSONObject.fromObject(body);
    Console.log("**********************************首次查询**********************************");
    Console.log(json2);
    Iterator iterator = json2.keys();
    while (iterator.hasNext()) {
      String key = (String) iterator.next();
      String value = json2.getString(key);
      Console.log("while11 " + key + " " + value);
    }
    Console.log("*************取结果*************");
    JSONArray json3 = JSONArray.fromObject(json2.getString("auto"));
    Iterator iterator2 = json3.iterator();
    while (iterator2.hasNext()) {
      Console.log("while12 " + iterator2.next());
    }
    Console.log("**********************************二次查询**********************************");
    List<Map<String, String>> l = new ArrayList<Map<String, String>>(json3);
    String temp = String.format("%.17f", Math.random());
    String urlStr2 = String.format("https://www.kuaidi100.com/query?type=%s&postid=%s&temp=%s", l.get(0).get("comCode"), id, temp);
    Console.log(urlStr2);
    HttpResponse res = HttpRequest.get(urlStr2).execute();
    String resFinal = res.body();
    JSONObject jsonFinal = JSONObject.fromObject(resFinal);
    Console.log("jsonFinal" + jsonFinal);
    Iterator iteratorFinal = jsonFinal.keys();
    while (iteratorFinal.hasNext()) {
      String key = (String) iteratorFinal.next();
      String value = jsonFinal.getString(key);
      Console.log("while21 " + key + " " + value);
    }
    /**********************************/
    JSONArray dataFinal = JSONArray.fromObject(jsonFinal.getString("data"));
    Iterator itFinal = dataFinal.iterator();
    Console.log("*************取结果*************");
    while (itFinal.hasNext()) {
      Console.log("while22 " + itFinal.next());
    }
  }
}
